package com.lbssoft.gpsbd.controller;

import com.lbssoft.gpsbd.annotation.LogAnnotation;
import com.lbssoft.gpsbd.model.Poi;
import com.lbssoft.gpsbd.model.Result;
import com.lbssoft.gpsbd.model.vo.UserVo;
import com.lbssoft.gpsbd.service.PoiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@Api("兴趣点管理")
@RestController
@RequestMapping("/pois")
public class PoiController {


	@Autowired
	private PoiService poiService;

	@ApiOperation(value="获取我的兴趣点", notes="获取我的兴趣点")
	@RequiresPermissions("user:poi:view")
	@RequestMapping(value = "",method = {RequestMethod.GET})
	@ResponseBody
	public Result list() {
		UserVo user = (UserVo) SecurityUtils.getSubject().getPrincipal();
		Result result = new Result();
		List<Poi> list = poiService.list(user.getId());
		result.setData(list);
		return result;
	}


	@ApiOperation(value="创建兴趣点", notes="创建兴趣点")
	@ApiImplicitParam(paramType="body",name = "poi", value = "创建兴趣点", dataType = "Poi")
	@LogAnnotation(targetType = "layer",action = "add",remark = "创建兴趣点")
	@RequiresPermissions("user:poi:add")
	@RequestMapping(value = "",method = {RequestMethod.POST})
	@ResponseBody
	public Result add(@Validated @RequestBody Poi poi) {
		return poiService.add(poi);
	}

	@ApiOperation(value="删除兴趣点", notes="删除兴趣点")
	@ApiImplicitParam(paramType="path",name = "id", value = "兴趣点id", required = true, dataType = "int")
	@LogAnnotation(targetType = "poi",action = "delete",remark = "删除兴趣点")
	@RequiresPermissions("user:poi:delete")
	@RequestMapping(value = "/{id}",method = {RequestMethod.DELETE})
	@ResponseBody
	public Result delete(@NotNull @PathVariable int id) {
		Result result = new Result();
		poiService.delete(id);
		return result;
	}

	@ApiOperation(value="修改兴趣点属性", notes="修改兴趣点属性")
	@ApiImplicitParam(paramType="body",name = "poi", value = "修改兴趣点属性", dataType = "Poi")
	@LogAnnotation(targetType = "poi",action = "update",remark = "修改兴趣点属性")
	@RequiresPermissions("user:poi:update")
	@RequestMapping(value = "",method = {RequestMethod.PUT})
	@ResponseBody
	public Result update(@Validated @RequestBody Poi poi) {
		Result result = new Result();
		poiService.update(poi);
		return result;
	}

}